package com.huanglj.himalaya.activitys;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.huanglj.himalaya.R;
import com.huanglj.himalaya.adapters.IndicatorAdapter;
import com.huanglj.himalaya.adapters.MainContentAdapter;
import com.huanglj.himalaya.data.dao.XimalayaDBHelper;
import com.huanglj.himalaya.interfaces.IPlayerCallBack;
import com.huanglj.himalaya.presenters.PlayerPresenter;
import com.huanglj.himalaya.utils.LogUtil;
import com.huanglj.himalaya.views.RoundRectImageView;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.category.Category;
import com.ximalaya.ting.android.opensdk.model.category.CategoryList;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends FragmentActivity implements IPlayerCallBack {
    private static final String TAG = "MainActivity";
    private MagicIndicator mMagicIndicator;
    private ViewPager mContentViewPager;
    private IndicatorAdapter mIndicatorAdapter;
    private ImageView mMainTrackCover;
    private TextView mMainTrackTitle;
    private TextView mMainTrackAuthor;
    private ImageView mMainPlay;
    private PlayerPresenter mPlayerPresenter;
    private View mMainPager;
    private ImageView mSearchButton;
    private View mMainBtnPlay;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //创建一个点击事件
        initEvent();
        initPresenter();
    }

    private void initPresenter() {
        mPlayerPresenter = PlayerPresenter.getPlayerPresenter();
        mPlayerPresenter.registerViewCallBack(this);
    }


    private void initEvent() {
        mIndicatorAdapter.setOnIndicatorTapClickListener(new IndicatorAdapter.OnIndicatorTapClickListener() {
            @Override
            public void onTabClick(int index) {
                LogUtil.d(TAG,"index ---------> " + index);
                mContentViewPager.setCurrentItem(index);
            }
        });
        mMainPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPlayerPresenter.isPlay()) {
                    mPlayerPresenter.pause();
                }else {
                    mPlayerPresenter.play();
                }
            }
        });
        mSearchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SearchActivity.class);
                startActivity(intent);
            }
        });
        mMainBtnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,PlayerActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        //title
        mMagicIndicator = findViewById(R.id.main_indicator);
        mMagicIndicator.setBackgroundColor(this.getResources().getColor(R.color.main_color));
        //创建一个item适配器
        mIndicatorAdapter = new IndicatorAdapter(this);
        CommonNavigator navigator = new CommonNavigator(this);
        //设置平分控件空间
        navigator.setAdjustMode(true);
        navigator.setAdapter(mIndicatorAdapter);


        //viewPager
        mContentViewPager = findViewById(R.id.content_pager);

        //适配器创建
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        MainContentAdapter mainContentAdapter = new MainContentAdapter(supportFragmentManager,1);
        mContentViewPager.setAdapter(mainContentAdapter);

        //把ViewPager和mMagicIndicator绑定
        mMagicIndicator.setNavigator(navigator);
        ViewPagerHelper.bind(mMagicIndicator,mContentViewPager);

        mMainTrackCover = findViewById(R.id.main_track_cover);
        mMainTrackTitle = findViewById(R.id.main_track_title);
        mMainTrackAuthor = findViewById(R.id.main_track_author);
        mMainPlay = findViewById(R.id.main_play_control);

        mSearchButton = findViewById(R.id.search_icon);
        mMainBtnPlay = findViewById(R.id.main_button_play);

    }

    @Override
    public void onPlayStart() {
        if (mMainPlay != null) {
            mMainPlay.setImageResource(R.mipmap.pause_normal);
        }
    }

    @Override
    public void onPlayPause() {
        if (mMainPlay != null) {
            mMainPlay.setImageResource(R.mipmap.play_normal);
        }
    }

    @Override
    public void onPlayStop() {
        if (mMainPlay != null) {
            mMainPlay.setImageResource(R.mipmap.play_normal);
        }
    }

    @Override
    public void onPlayError() {

    }

    @Override
    public void onNextPlay(Track track) {

    }

    @Override
    public void onPrePlay(Track track) {

    }

    @Override
    public void onListLoaded(List<Track> list) {

    }

    @Override
    public void onPlayModeChange(XmPlayListControl.PlayMode mode) {

    }

    @Override
    public void onProgressChange(int currentProgress, int total) {

    }

    @Override
    public void onAdLoading() {

    }

    @Override
    public void onAdFinished() {

    }

    @Override
    public void onTrackUpdate(Track track, int playIndex) {
        int dip2px = UIUtil.dip2px(this, 68);
        View viewById = findViewById(R.id.main_button_play);
        if (track != null) {
            //当播放的时候，设置margin值
            setParams(dip2px, viewById);
            String trackTitle = track.getTrackTitle();
            String nickname = track.getAnnouncer().getNickname();
            String coverLarge = track.getCoverUrlLarge();
            Glide.with(this).load(coverLarge).into(mMainTrackCover);
            mMainTrackTitle.setText(trackTitle);
            mMainTrackAuthor.setText(nickname);
        }else {
            viewById.setVisibility(View.GONE);
        }
    }

    private void setParams(int dip2pxEnd, View viewById) {
        int dip2pxTop = UIUtil.dip2px(this, 40);
        viewById.setVisibility(View.VISIBLE);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        layoutParams.setMargins(0,0,0,dip2pxEnd);
        layoutParams.addRule(RelativeLayout.BELOW,R.id.this_is_title);
        mContentViewPager.setLayoutParams(layoutParams);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPlayerPresenter.unRegisterViewCallBack(this);
    }
}
